package com.atguigu.tingshu.payment.service.impl;
import java.math.BigDecimal;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;

import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private KafkaService kafkaService ;

    @Transactional
    @Override
    public void savePaymentInfo(OrderInfo orderInfo) {

        // 创建PaymentInfo对象
        PaymentInfo paymentInfo = new PaymentInfo() ;
        paymentInfo.setUserId(orderInfo.getUserId());
        paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_ORDER);
        paymentInfo.setOrderNo(orderInfo.getOrderNo());
        paymentInfo.setPayWay(orderInfo.getPayWay());
        // paymentInfo.setOutTradeNo("");                          // 保存微信端针对本次交易所生成的交易号
        paymentInfo.setAmount(orderInfo.getOrderAmount());
        paymentInfo.setContent(orderInfo.getOrderTitle());
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);

        // 保存交易数据
        this.save(paymentInfo) ;


    }

    @Transactional
    @Override
    public void updatePaymentInfo(Transaction transaction) {

        // 修改交易信息的支付状态
        log.info(JSON.toJSONString(transaction));
        String orderNo = transaction.getOutTradeNo();
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo , orderNo) ;
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);
        if(SystemConstant.PAYMENT_STATUS_PAID.equals(paymentInfo)) return ;

        // 修改支付状态为已支付
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);
        paymentInfo.setOutTradeNo(transaction.getTransactionId());
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(transaction));
        this.updateById(paymentInfo) ;

        // 修改订单状态的状态，并且需要保存购买信息
        String paymentType = paymentInfo.getPaymentType();
        if(SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)) {     // 订单支付成功了
            kafkaService.sendMsg(KafkaConstant.QUEUE_ORDER_PAY_SUCCESS , orderNo);
        }else if(SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentType)) {
            kafkaService.sendMsg(KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS , orderNo);
        }

    }

    @Override
    public void savePaymentInfo(RechargeInfo rechargeInfo) {

        // 创建PaymentInfo对象
        PaymentInfo paymentInfo = new PaymentInfo() ;
        paymentInfo.setUserId(rechargeInfo.getUserId());
        paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_RECHARGE);
        paymentInfo.setOrderNo(rechargeInfo.getOrderNo());
        paymentInfo.setPayWay(rechargeInfo.getPayWay());
        // paymentInfo.setOutTradeNo("");                          // 保存微信端针对本次交易所生成的交易号
        paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
        paymentInfo.setContent("充值" + rechargeInfo.getRechargeAmount() + "元");
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);

        // 保存交易数据
        this.save(paymentInfo) ;


    }

}
